﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace DienLib.SummitLib
{
    /// <summary>
    /// Wrapper for Infragistics UltraExplorerBar in Summit Main Form
    /// </summary>
    public class UltraExplorerBar
    {
        public enum UltraExplorerBarAction
        {
            /*ActivateAndCheckFirstCurrentlyVisibleGroupOrItem,
            ActivateAndCheckFirstGroup,
            ActivateAndCheckFirstItemInNextGroup,
            ActivateAndCheckFirstItemInSameGroup,
            ActivateAndCheckLastCurrentlyVisibleGroupOrItem,
            ActivateAndCheckLastGroup,
            ActivateAndCheckLastItemInPreviousGroup,
            ActivateAndCheckLastItemInSameGroup,
            ActivateAndCheckNextGroupOrItem,
            ActivateAndCheckPreviousGroupOrItem,
            ActivateFirstCurrentlyVisibleGroupOrItem,
            ActivateFirstCurrentlyVisibleItem,*/
            ActivateFirstGroup = 7,
            /*ActivateFirstGroupOrItem,
            ActivateFirstItemInActiveGroup,
            ActivateFirstItemInFirstGroup,
            ActivateFirstItemInNextGroup,
            ActivateFirstItemInPreviousGroup,
            ActivateFirstItemInSameGroup,
            ActivateGroupContainingActiveItem,
            ActivateLastCurrentlyVisibleGroupOrItem,
            ActivateLastCurrentlyVisibleItem,*/
            ActivateLastGroup = 8,
            /*ActivateLastGroupOrItem,
            ActivateLastItemInActiveGroup,
            ActivateLastItemInLastGroup,
            ActivateLastItemInNextGroup,
            ActivateLastItemInPreviousGroup,
            ActivateLastItemInSameGroup,
            ActivateNextGroup,
            ActivateNextGroupOrItem,
            ActivateNextItem,
            ActivatePreviousGroup,
            ActivatePreviousGroupOrItem,
            ActivatePreviousItem,
            ActivateQuickCustomizeButton,
            CheckActiveItem,*/
            ClickActiveGroup = 27,
            /*ClickActiveItem,
            CloseNavigationPaneFlyoutCancel,
            CloseNavigationPaneFlyoutCommit,
            DisplayQuickCustomizeMenu,
            DoNothing,
            EditActiveGroup,
            EditActiveItem,
            ExitEditMode,
            ScrollPageDown,
            ScrollPageDownThenActivateAndCheckLastVisibleGroupOrItem,
            ScrollPageUp,
            ScrollPageUpThenActivateAndCheckFirstVisibleGroupOrItem,
            ScrollToBottom,
            ScrollToTop,
            SelectActiveGroup,
            TabNextFromActiveGroupOrItem,
            TabPreviousFromActiveGroupOrItem,
            ToggleActiveGroupExpansion*/
        };
        public struct CUST
        {
            public string DisplayName { get; private set; }
            public string ClientName { get; private set; }
            public string ClientSAP { get; private set; }
            public string OID { get; private set; }
            private CUST(string displayName, string clientName, string clientSAP, string OID)
                : this()
            {
                this.DisplayName = displayName;
                this.ClientName = clientName;
                this.ClientSAP = clientSAP;
                this.OID = OID;
            }

            /// <summary>
            /// When create this should use Invoke from Parent Control because will read some Property CrossThread
            /// </summary>
            /// <param name="cust"></param>
            public CUST(object cust) : this(toDisplayName(cust), toClientName(cust), toClientSAP(cust), toOID(cust)) { }

            private static string toDisplayName(object cust)
            {
                return DienLib.Reflector.GetProperty<string>(cust, "DisplayName");
            }

            private static string toClientName(object cust)
            {
                return DienLib.Reflector.GetProperty<string>(cust, "NM");
            }

            private static string toClientSAP(object cust)
            {
                return DienLib.Reflector.GetProperty<string>(cust, "OWN_PRVDR_KEY");
            }

            private static string toOID(object cust)
            {
                return DienLib.Reflector.GetProperty<string>(cust, "OID");
            }
        }

        private Control _refobj;
        //const string CUSTOMERVIEW_LISTBOX = "listBox1";
        ListBox a_listBox1;

        public UltraExplorerBar(Control refUltraExplorerBar)
        {
            _refobj = refUltraExplorerBar;
        }

        public void PerformCustomerClick(Enums.Customer customer)
        {
            if (_refobj.InvokeRequired) _refobj.Invoke((MethodInvoker)delegate() { delCustomerClick(customer); });
            else delCustomerClick(customer);
        }

        private void delCustomerClick(Enums.Customer customer)
        {
            DienLib.Reflector.DoMethod<bool>(_refobj, "PerformAction", new object[] { UltraExplorerBarAction.ActivateFirstGroup });
            DienLib.Reflector.DoMethod<bool>(_refobj, "PerformAction", new object[] { UltraExplorerBarAction.ClickActiveGroup });
            a_listBox1 = _refobj.Controls[0].Controls[0].Controls[0].Controls[0] as ListBox;

            //Thread.Sleep(5000); // To Pause for a while & see "Loading ..."
            PopUp.CountDownWPF.ShowDialog(5, "SUMMIT take time to load CUSTOMER VIEW !\r\n\"PLEASE WAIT FOR SOME SECONDS !\"", "WAIT FOR LOADING CUSTOMER VIEW ...", delegate(object sender)
                {   // DELEGATE METHOD
                    if (a_listBox1 == null) return false;
                    if (a_listBox1.Items[0].ToString().ToLower().Contains("loading")) return false;
                    return true; // Stop Count down
                    // END DELEGATE
                });

            bool duplicate = false;
            for (int i = 0; i < a_listBox1.Items.Count; i++)
            {
                CUST cust = new CUST(a_listBox1.Items[i]);
                //MessageBox.Show(cust.DisplayName + "\r\n" + Enums.eCustomer.ToView(customer));
                if (cust.DisplayName == Enums.eCustomer.ToView(customer))
                {   // Dung thang nay thi chon no thoi
                    if (!duplicate) { a_listBox1.SetSelected(i, true); duplicate = true; }
                    else PopUp.PopUpOK.ShowDialog("Duplicate Customer Display Name, Something wrong", "DUPLICATE CUSTOMER NAME & SAP NUMBER");
                }
            }

            // More code for implementing 
        }

        #region DISPOSE METHOD
        // Dispose() calls Dispose(true)
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        // NOTE: Leave out the finalizer altogether if this class doesn't 
        // own unmanaged resources itself, but leave the other methods
        // exactly as they are. 
        ~UltraExplorerBar()
        {
            // Finalizer calls Dispose(false)
            Dispose(false);
        }

        // The bulk of the clean-up code is implemented in Dispose(bool)
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // free managed resources, (detach event ?)
                if (this._refobj != null) this._refobj = null; // Khong keu this._refobj.Dispose vi Reference toi object do. Ngoai ra do pass reference nen this._refobj = null khong anh huong gi toi object goc
                if (this.a_listBox1 != null) this.a_listBox1 = null;
            }

            /* free native resources if there are any.
            if (nativeResource != IntPtr.Zero) 
            {
                Marshal.FreeHGlobal(nativeResource);
                nativeResource = IntPtr.Zero;
            }*/
        }
        #endregion

#if DEBUG
        public void Test()
        {
            //Must use Invoke in real code to avoid crossthread
            DienLib.Reflector.DoMethod<bool>(_refobj, "PerformAction", new object[] { UltraExplorerBarAction.ActivateFirstGroup });
            DienLib.Reflector.DoMethod<bool>(_refobj, "PerformAction", new object[] { UltraExplorerBarAction.ClickActiveGroup });
            foreach (object cust in a_listBox1.Items)
            {
                CUST obj = new CUST(cust);
                MessageBox.Show(obj.DisplayName);
            }
        }
#endif
    }
}